package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import com.atguigu.gmall.mq.service.RabbitService;
import io.swagger.annotations.Api;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author Administrator
 * @create 2020-05-29 19:12
 */
@RestController
public class TestMessageSendController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitService rabbitService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //发送消息
    @GetMapping("/sendMessage")
    public Result sendMessage(){
        //交换机   routingKey  消息内容
        rabbitService.sendMessage("exchange11","routingKey11","haha");
        return Result.ok();
    }

    //发送消息  基于死信的延迟消息
    @GetMapping("sendDeadLetterMessage")
    public Result sendDeadLetterMessage(){
        rabbitTemplate.convertAndSend(DeadLetterMqConfig.exchange_dead,
                                      DeadLetterMqConfig.routing_dead_1,
                                      "111111",(message)->{
                                            message.getMessageProperties().setExpiration("8000");
                                            System.out.println("发送延迟消息的时间：" + new Date());
                                            return message;
                                      });

        return Result.ok();
    }


    //发送消息  基于插件的延迟消息
    @GetMapping("sendDelayMessage")
    public Result  sendDelayMessage(){
       /* rabbitTemplate.convertAndSend(
                DelayedMqConfig.exchange_delay,
                DelayedMqConfig.routing_delay,
                "基于插件的延迟：11222",(message)->{
                    message.getMessageProperties().setDelay(10000);
                    System.out.println("发送延迟消息的时间：" + new Date());
                    return message;
                });*/
        rabbitService.sendDelayedMessage(DelayedMqConfig.exchange_delay,DelayedMqConfig.routing_delay
                ,"1321313131313131",10000);
        return Result.ok();
    }

}
